import { apiInquile } from "@/api/area";
// 获取部门信息
import { apiGetDepartment } from "@/api/common.js";
import { mapState } from "vuex";
import { apigetUnit } from "@/api/unit";
import { sex,userIdentity } from "@/utils/options";
import { queryUnitInfo,queryDepartmentInfo } from "@/api/register";

import { queryAreasGetUnitInfo } from "@/api/user";
export default {
  data() {
    return {
      cascaderValue: [],
      // 显示性别选择
      visibleSex: false,
      visibleUserIdentity: false,
      //选择的性别
      currentSex: "",
      // 选择的性别id
      sexId: "",
      //区域列表
      areaList: [],
      // 选择的区域
      currentArea: "",
      tempArea:'',
      // 省
      province_id: "",
      // 市
      city_id: "",
      // 区
      area_id: "",
      // 选择所属单位
      currentUnit: "",
      // 单位列表
      unitList: [],
      // 所属部门列表
      departmentList: [],
      // 选择的部门
      currentDepartment: "",
      // 部门编号
      department: "",
      // 是否显示性别
      visibleSex: false,
      // 显示区域的选择器
      visibleArea: false,
      // 显示单位的选择器
      visibleUnit: false,
      // 显示部门的选择器
      visibleDepartment: false,
    };
  },

  methods: {
    async getUnit(par) {
    let res=  await queryUnitInfo(par);
    this.unitList = res.data;
    },
    // 获取区域
    async getArea() {
      try {
        let res = await apiInquile();
        this.areaList = res.data.map((item) => {
          const children = Array.isArray(item.child)
            ? item.child.map((childItem) => {
                const children2 = Array.isArray(childItem.child)
                  ? childItem.child.map((grandChildItem) => {
                      return {
                        text: grandChildItem.name,
                        value: grandChildItem.id,
                      };
                    })
                  : [];

                return {
                  text: childItem.name,
                  value: childItem.id,
                  children: children2,
                };
              })
            : [];

          return {
            text: item.name,
            value: item.id,
            children: children,
          };
        });
      } catch (error) {
        this.areaList = [];
      }
    },
    // 获取部门
    async getDepartment(id) {
      
      let res= await queryDepartmentInfo({id});
      if (res.code == 200) {
      this.departmentList = res.data.department.map((item) => {
        const children = Array.isArray(item.child)
          ? item.child.map((childItem) => {
              const children2 = Array.isArray(childItem.child)
                ? childItem.child.map((grandChildItem) => {
                    return {
                      text: grandChildItem.name,
                      value: grandChildItem.id,
                    };
                  })
                : [];

              return {
                text: childItem.name,
                value: childItem.id,
                children: children2,
              };
            })
          : [];

        return {
          text: item.name,
          value: item.id,
          children: children,
        };
      });
      }
    },
    // 递归处理部门数组
    recursion(obj) {
      let result = obj;
      for (let i = 0; i < result.length; ++i) {
        const newObj = {
          text: result[i].unit_name
            ? result[i].unit_name
            : result[i].department_name,
          value: result[i].id ? result[i].id : result[i].platform_unit_id,
        };
        if (result[i].children) {
          result[i].children.length != 0
            ? (newObj.children = result[i].children)
            : delete newObj.children;
          obj[i] = newObj;

          if (obj[i].children && obj[i].children.length != 0)
            this.recursion(obj[i].children);
        }
      }
    },
    resetArea(){
      this.visibleArea=false
      this.currentArea=''
      this.currentUnit={}
      this.currentDepartment=''
    },
    resetUnit(){
      this.visibleUnit=false
      this.currentUnit=''
       this.currentDepartment=''
    },
    resetDepartment(){
      this.visibleDepartment=false
      this.currentDepartment=''
    },
    changeArea({value, selectedOptions, tabIndex }){
    },
    checkUnit(){
      if(!this.currentArea){
        return this.$toast.fail("请先选择区域！");
      }
      this.visibleUnit=true
    },
    checkDepartment(){
      if(!this.currentArea){
        return this.$toast.fail("请先选择区域！");
      }
      if(!this.currentUnit.name){
        return this.$toast.fail("请先选择单位！");
      }else{
        this.visibleDepartment=true;
      }
    },
    selectArea({ value, selectedOptions }) {
      this.currentUnit={}
      this.currentDepartment=''
      this.visibleArea = false;
      this.currentArea = selectedOptions.map((option) => option.text).join("/");

      try {
        if (selectedOptions.length > 0) {
          this.province_id = selectedOptions[0].value;
        }
        if (selectedOptions.length > 1) {
          this.city_id = selectedOptions[1].value;
        }
        if (selectedOptions.length > 2) {
          this.area_id = selectedOptions[2].value;
        }
      } catch (error) {
        console.error("区域选择赋值出错:", error);
      }

      let par = {
        province_id: this.province_id,
        city_id: this.city_id,
        area_id: this.area_id,
      };
      this.getUnit(par);
    },
    // 选择单位
    selectUnit($event) {
      if (!$event) return;
      this.visibleUnit = false;
      this.currentUnit = $event;

      this.getDepartment(this.currentUnit.id);
      this.currentDepartment = "";
      this.department = "";
    },
    // 选择部门
    selectDepartment({ selectedOptions }) {
      this.visibleDepartment = false;
      this.currentDepartment = selectedOptions
        .map((option) => option.text)
        .join("/");
      this.department = selectedOptions[selectedOptions.length - 1].value;
    },
    // 选择性别
    selectSex(e) {
      this.currentSex = e;
      this.visibleSex = false;
    },
  },
  computed: {
    sexs() {
      return sex;
    },
    userIdentitys() {
      return userIdentity;
    },
    ...mapState(["INFO"]),
  },
  created() {
    this.getArea();
  },
};
